<?php
namespace app\api\controller;

use  think\Controller;
use app\common\model\NewsType;
use app\api\model\News as NewsModel;
use think\Db;


class News extends Controller
{
    // 新闻类型
    public function types(){
        $list= NewsType::where('state',1)->select();
        json(200,'成功',$list);
    }
    //新闻列表
    public function newslist($type=0){
     //   $list=NewsModel::where('type_id',$type)->select();
        $model=new NewsModel();
        $list=$model->getList($type);
        json(200,'成功',$list);
    }

    public function newsall()
    {
        $types = NewsType::where('state', 1)->select();
        $model = new NewsModel();
        $res = [];
        foreach($types as $type) {
            $type['content'] = $model->getList($type['id']);
            $res[] = $type;
        }
        json(200,'成功',$res);
    }
  //新闻详情
  public function newsdetail($id=0){
    $list=NewsModel::with('type')->where('id',$id)->find();
    json(200,'成功',$list);
}
    //删除新闻
    public function newsdel($id=0){
        $reg=NewsModel::destroy($id);
        json(200,'成功');
    }

    //获取新增用户数据给echarts表

    public function countUserByDay($day='2020-12-28'){
        $start_time=strtotime($day);
        $end_time=$start_time+86400;
        return Db::name('user')
            ->where('create_time','>=',$start_time)
            ->where('create_time','<',$end_time)
            ->count();
    }

    public function countUser($days=[]){
        $res=[];
        foreach ($days as $day){
            $res[]=$this->countUserByDay($day);
        }
        return $res;
    }
    //传递数据给前端
    public function market(){
        $list=[
            'days'=>getLately7days(),
            'count'=>$this->countUser(getLately7days())
        ];
        json(200,'成功',$list);
    }


    public function test(){
        $secs=24*3600;
        $t=date('Y-m-d',time()-86400);
        $days=getLately7days();
        $c1=$this->countUserByDay();
        $cs=$this->countUser(getLately7days());
        $data=compact('t','secs','days','c1','cs');
        var_dump($data);
    }
}